package net.zjitc.dao.impl;

import net.zjitc.dao.StudentDao;
import net.zjitc.domain.Student;

import net.zjitc.utils.JdbcUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class StudentDaoImpl implements StudentDao {

    //使用JdbcTemplate注入的是数据源
    private JdbcTemplate template = new JdbcTemplate(JdbcUtils.getDataSource());

    public int add(Student student) {
        String sql = "INSERT INTO student(name,age,gander,address,birthday) VALUES (?,?,?,?,?)";
        Object[] args = {student.getName(),student.getAge(),student.getGander(),student.getAddress(),student.getBirthday()};
        int result = template.update(sql,args);
        return result;
    }

    public boolean update(Student student) {
        return false;
    }

    public boolean delete(int index) {
        return false;
    }

    public Student findByName(String name) {
        String sql = "SELECT * FROM student where name= ?";
        Student student =template.queryForObject(sql,new BeanPropertyRowMapper<Student>(Student.class),name);
        return student;
    }

    public List<Student> findAll() {
        //2.写sql语句(查询所有学生)
        String sql = "SELECT * FROM student";
        List<Student> list = template.query(sql,new BeanPropertyRowMapper(Student.class));
        return list;
    }
}
